package groups.diseases.daobak;

import groups.diseases.DiseaseCausesSymptom;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

public class JDBCDiseaseCausesSymptomDAO implements IDiseaseCausesSymptomDAO {
	private Connection connection;
	
	public JDBCDiseaseCausesSymptomDAO (Connection connection) throws SQLException{
		this.connection = connection;
	}
	
	@Override
	public DiseaseCausesSymptom create(int diseaseId, int humanSymptomId) throws SQLException {
		Statement stm = connection.createStatement();
		
		if(getItem(diseaseId, humanSymptomId)==null)		
			stm.executeUpdate("insert into DiseaseCausesSymptom values("+
					diseaseId+","+
					humanSymptomId+")");
		
		stm.close();
		
		return new DiseaseCausesSymptom(diseaseId, humanSymptomId);

	}

	

	@Override
	public void delete(DiseaseCausesSymptom symp) throws SQLException {
		Statement stm = connection.createStatement();
		
		stm.executeUpdate("delete from DiseaseCausesSymptom where "+
		" diseaseId=" + symp.getDiseaseId()+
		"and HumanSymptomId=" + symp.getHumanSymptomId());
		
	}

	@Override
	public Vector<DiseaseCausesSymptom> getList() throws SQLException {
		Statement stm = connection.createStatement();
		
		ResultSet res = stm.executeQuery("select * from DiseaseCausesSymptom");
		
		Vector<DiseaseCausesSymptom> vec = new Vector<DiseaseCausesSymptom>();
		
		while(res.next())
			vec.add(new DiseaseCausesSymptom(res.getInt(1), res.getInt(2)));
		
		return vec;
		
	}
	
	public Vector<DiseaseCausesSymptom> getList(int id) throws SQLException {
		Statement stm = connection.createStatement();
		
		ResultSet res = stm.executeQuery("select * from DiseaseCausesSymptom where diseaseId="+id);
		
		Vector<DiseaseCausesSymptom> vec = new Vector<DiseaseCausesSymptom>();
		
		while(res.next())
			vec.add(new DiseaseCausesSymptom(res.getInt(1), res.getInt(2)));
		
		return vec;
		
	}

	@Override
	public DiseaseCausesSymptom getItem(int diseaseId, int symptomId) throws SQLException {
		Statement stm = connection.createStatement();
		
		ResultSet res = stm.executeQuery("select * from DiseaseCausesSymptom where "+
		" DiseaseId=" +diseaseId+
		"and HumanSymptomId=" + symptomId);
		
		if(res.next())
			return new DiseaseCausesSymptom(diseaseId, symptomId);
		else
			return null;
	}


}
